---
layout: base
title: Creation
---

<header class="jumbotron subhead" id="overview">
  <div class="container">
    <h3>Entities and Relationships</h3>
  </div>
</header>

<div class="container">
  <div class="row">
    <div class="col-md-3">
      <div id="sidebar" role="complementary" data-spy="affix" data-offset-top="80">
        <ul class="nav sidebar">
          <li class="active"><a href="#define">Defining Entities</a></li>
          <li><a href="#how-help">Adding Properties</a></li>
          <li><a href="#why">Managing Relationships</a></li>
          <li><a href="#what">Basic Usage</a></li>
        </ul>
      </div>
    </div>
    <div class="col-md-9">
        <section id="define">
          <div class="page-header">
            <h1>1. Defining Entities</h1>
          </div>
          <p class="lead">To create a persistent entity, just extend it with <code>SugarRecord</code>.</p>
          <div class="bs-docs-example">
{% highlight java %}
public class Book extends SugarRecord<Book> {
  String name;
}
{% endhighlight %}
          </div>
          <p>This class would automatically map to a table named <code>book</code>.
        </section>

        <section id="how-help">
          <div class="page-header">
            <h1>2. Adding properties.</h1>
          </div>
          <p class="lead">Now that you have the entity, start defining their properties.</p>

          <div class="bs-docs-example">
{% highlight java %}
public class Book extends SugarRecord<Book> {
  String name;
  String ISBN;
  String title;
  String shortSummary;
}
{% endhighlight %}
          </div>

          <p>This would create corresponding columns in the <code>book</code> table. Column names would be <code>name, ISBN, title and short_summary</code>. Notice the conversion from <code>shortSummary</code> to <code>short_summary</code>. This is the convention followed in Sugar. (Next step: making it configurable.)</p>

          <p class="lead">To skip a property from persisting, annotate it as <code>Ignore</code>.</p>
          <p>In below example, <code>name</code> would not be persisted, neither would a corresponding column be created for this property.</p>

          <div class="bs-docs-example">
{% highlight java %}
public class Book extends SugarRecord<Book> {
  @Ignore
  String name;

  String ISBN;
}
{% endhighlight %}
          </div>
        </section>

        <section id="why">
          <div class="page-header">
            <h1>3. Managing relationships</h1>
          </div>
          <p class="lead">Lets bring another entity into picture.</p>
          <div class="bs-docs-example">
{% highlight java %}
public class Author extends SugarRecord<Author> {
  String name;
}
{% endhighlight %}
          </div>

          <p class="lead">Each book has an author and that would be represented by having a reference to Author in Book class, as follows:</p>

          <div class="bs-docs-example">
{% highlight java %}
public class Book extends SugarRecord<Book> {
  String name;
  String ISBN;
  String title;
  String shortSummary;

  // defining a relationship
  Author author;
}
{% endhighlight %}
          </div>
          <p class="lead">This would store a column named <code>author</code> in the book table. This would help with one-to-one and one-to-many relationships.</p>
        </section>

        <section id="what">
          <div class="page-header">
            <h1>4. Basic Usage</h1>
          </div>

          <div class="bs-docs-example">
{% highlight java %}
List<Book> books = Book.find(Book.class, "author = ?", new String{author.getId()});
{% endhighlight %}

{% highlight java %}
Book book = Book.findById(Books.class, 1);
Author author = book.author;
{% endhighlight %}
        </div>
      </section>

    </div>
  </div>
</div>
